package com.runa.monitor.platinterface.backwork.impl;

import com.runa.monitor.comm.constants.ResultConstants;
import com.runa.monitor.comm.dto.newComm.AsynResult;
import com.runa.monitor.comm.dto.newComm.NetCurrentData;
import com.runa.monitor.comm.entity.CommNetEquipment;
import com.runa.monitor.platinterface.backwork.function.NetStatusCallback;
import com.runa.monitor.platinterface.extend.hdb.CollectionUtil;
import com.runa.monitor.platinterface.mqtt.MqttUtil;
import com.runa.monitor.platinterface.platService.RepositoryService;
import com.runa.rccprotocol.dto.equdata.ConcenReportData;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;

/**
 * @author zhuj
 */
@Slf4j
public class NetStatusCallbackImpl implements NetStatusCallback<String> {

    private final Executor executor;

    private final RepositoryService repositoryService;

    private final Map<String, String> versionMap = new ConcurrentHashMap<>();

    private final Map<String, String> iccidMap = new ConcurrentHashMap<>();

    public NetStatusCallbackImpl(Executor executor, RepositoryService repositoryService) {
        this.executor = executor;
        this.repositoryService = repositoryService;
    }

    @Override
    public void rccStatusChange(List<String> serials, boolean status) {
        if (CollectionUtil.isNotEmpty(serials)) {
            executor.execute(() -> repositoryService.getCommNetEquipmentByNetSerial(serials)
                    .forEach(equipment -> MqttUtil.sendOnLineStateOfNoneContent(equipment, status)));
        }
    }

    @Override
    public void rccStatusChange(String serial, boolean status) {
        executor.execute(() -> {
            CommNetEquipment commNetEquipment = repositoryService.getCommNetEquipmentByNetSerial(serial);
            if (commNetEquipment != null) {
                MqttUtil.sendOnLineStateOfNoneContent(commNetEquipment, status);
            }
        });
    }

    @Override
    public void rccNetEquReport(String serial, ConcenReportData reportData) {
        if (reportData != null) {
            NetCurrentData currentData = new NetCurrentData();
            currentData.setDate(reportData.getDate());
            currentData.setSignValue(reportData.getCsq());
            currentData.setElectric(reportData.getElectric());
            if (reportData.getIccid() != null) {
                iccidMap.put(serial, reportData.getIccid());
                currentData.setIccid(reportData.getIccid());
            } else if (iccidMap.get(serial) != null) {
                currentData.setIccid(iccidMap.get(serial));
            }
            if (reportData.getVer() != null) {
                versionMap.put(serial, reportData.getVer());
                currentData.setVer(reportData.getVer());
            } else if (versionMap.get(serial) != null) {
                currentData.setVer(versionMap.get(serial));
            }
            currentData.setSignValue(reportData.getCsq());
            currentData.setEvent(reportData.getEvent());
            AsynResult asynResult = AsynResult.builder()
                    .code(ResultConstants.CODE_200)
                    .msg(MqttUtil.successString)
                    .identity(Objects.requireNonNull(repositoryService.getCommNetEquipmentByNetSerial(serial)).getId()).build();
            asynResult.setContent(currentData);
            log.info("网络设备主动上报信息: {}", asynResult);
            MqttUtil.send(currentData.getDataType(), asynResult);
        }
    }

    @Override
    public void rccNetEquReport(String serial, ConcenReportData reportData, String taskNo) {
        if (reportData != null) {
            NetCurrentData currentData = new NetCurrentData();
            currentData.setDate(reportData.getDate());
            currentData.setSignValue(reportData.getCsq());
            currentData.setElectric(reportData.getElectric());
            currentData.setIccid(reportData.getIccid());
            currentData.setVer(reportData.getVer());
            currentData.setSignValue(reportData.getCsq());
            currentData.setEvent(reportData.getEvent());
            AsynResult asynResult = AsynResult.builder()
                    .code(ResultConstants.CODE_200)
                    .msg(MqttUtil.successString)
                    .identity(serial).build();
            asynResult.setContent(currentData);
            log.info("瑞纳电表信息数据: {}", asynResult);
            MqttUtil.send(asynResult);
        }
    }
}
